import sqlite3


class DataBase:

    def __init__(self,seq_name:str,db_path:str) -> None:
        self.seq_name = seq_name
        self.db_path = db_path
        self.data_list = []
        self._load_db()
        pass

    def _load_db(self):
        conn = sqlite3.connect(self.db_path)
        c = conn.cursor()
        print ("数据库打开成功")
        cursor = c.execute('select * from gt;')
        for row in cursor:
            # print(row)
            # (342, 4, 959, 376, 110, 107, 'commodity', 1)
            self.data_list.append(row)
            # frameid,inclasid,x,y,w,h,cname,cid = row
        conn.close()

    def __iter__(self):
        self.idx = 0
        self.lenth = len(self.data_list)
        return self
    
    def __next__(self):
        if self.idx == self.lenth:
            raise StopIteration
        row = self.data_list[self.idx]
        self.idx += 1
        frameid,inclasid,x,y,w,h,cname,cid = row
        unique_name = f"{self.seq_name}-{cname}-{inclasid}"
        # 注意数据集中存在-1这种错误坐标参数
        return unique_name,self.seq_name,int(frameid),int(inclasid),\
            max(0,int(x)),max(0,int(y)),int(w),int(h),cname,int(cid)


